package com.wqtz.chechai.adapter;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/2/10 14:09
 * 图片浏览ViewPageAdapter
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.wqtz.chechai.R;

import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

public class CarBookViewPageAdapter extends PagerAdapter {
	private Context context;
	private List<String> images;
	private SparseArray<View> cacheView;
	private ViewGroup containerTemp;

	public CarBookViewPageAdapter(Context context, List<String> images) {
		this.context = context;
		this.images = images;
		cacheView = new SparseArray<>(images.size());
	}

	@Override
	public Object instantiateItem(final ViewGroup container, int position) {
		if(containerTemp == null) containerTemp = container;

		View view = cacheView.get(position);

		if(view == null){
			view = LayoutInflater.from(context).inflate(R.layout.item_car_book_image,container,false);
			view.setTag(position);
			final ImageView image = (ImageView) view.findViewById(R.id.image);
			final PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(image);

			Picasso.with(context).load(images.get(position)).config(Bitmap.Config.RGB_565).into(image, new Callback() {
				@Override
				public void onSuccess() {
					photoViewAttacher.update();
				}

				@Override
				public void onError() {

				}
			});

			photoViewAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
				@Override
				public void onPhotoTap(View view, float x, float y) {
					Activity activity = (Activity) context;
					activity.finish();
				}

			});
			cacheView.put(position,view);
		}
		container.addView(view);
		return view;
	}

	@Override
	public int getCount() {
		return images.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		View view = (View) object;
		container.removeView(view);
	}

}
